#ifndef __MISC_LIB_
#define __MISC_LIB_

#include <string>
#include <mpParser.h>
#include "datatypes.h"
#include <signal.h>

using namespace std;
using namespace mup;

void throw_uncaught_exception(const char * msg, const char * funcname, const char * fname, int line_number);
#define fatal_stop(msg) { throw_uncaught_exception(msg, __PRETTY_FUNCTION__, __FILE__, __LINE__); }
#define fatal_stop_nested(msg, funcname, file, linenumber) { throw_uncaught_exception(msg, funcname, file, linenumber); }
#define rassert(test) { if(! (test) ){ throw_uncaught_exception("Run-time assertion failed.", __PRETTY_FUNCTION__,__FILE__,__LINE__); } }

#ifdef DEBUG
	#define dassert(test, msg) { if( !(test) ){ fprintf(stderr, "%s\n", msg); \
	throw_uncaught_exception("Assertion failed.", __PRETTY_FUNCTION__,__FILE__,__LINE__); } }
#else
	#define dassert(test,msg)
#endif

bool src_matchkeyword_aux(const string & keyword, ...);
#define matchkeyword(keyword, ...) src_matchkeyword_aux(keyword,## __VA_ARGS__ , NULL) 

#ifdef DEBUG
void call_debug_trap(); // global scope trap function for debugging
#else
#define call_debug_trap() 
#endif

int stdsprintf(string & dest, const char * fmt, ...);
int stdvsprintf(string & dest, const char * fmt, va_list args);

class global_flag {
	volatile sig_atomic_t flag;
	public:
	global_flag();
	const sig_atomic_t read() const;
	void set(sig_atomic_t);
	void toggle();
	void reset();
};

global_flag & gbl_SIG_INT();

void check_for_signals(); // throws an exception if status if bad
#define SIG_INT_EXCEPTION_NUMBER 143
bool is_exception_SIG_INT(const cgcmexception&);

const string & return_string();
bool breakpoint();
bool isVariableLegal(const char *);
bool isStringReal(const char *);
bool isStringString(const char *);
bool isStringInteger(const char *);
int countChars(const char *, const char, int spos, int epos);
bool isVariableDefined(const string & name, const ParserX & p);
bool isTokenDefined(const string & name, const ParserX & p);


void forceDefineVar(ParserX & , const string &, Value *);

string convertfloat2string(const float &);
string convertcomplex2string(const qgcomplex &);
int convertstring2integer(const string &);
float convertstring2float(const string &);
string converthandle2string(const int &);
string convertint2string(const int &);
string removequotes(const string & str);




#endif
